package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    public static String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    public static String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    public static String ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    @Value("${wechat.appId}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;

    /**
     * 封装公共参数
     */
    public Map<String,Object> getAppConfig() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        return paramMap;
    }


        /**
         * 获取openid
         *
         * @param code
         * @return
         */
    @Override
    public String getOpenid(String code) {
//        GET https://api.weixin.qq.com/sns/jscode2session
//        String OPENID_URL="https://api.weixin.qq.com/sns/jscode2session";
        //1、封装Http请求参数
        Map<String, Object> dataMap = getAppConfig();
        dataMap.put("js_code", code);
        dataMap.put("grant_type", "authorization_code");
        //2、使用糊涂工县实现远程调用
        String result = HttpUtil.get(OPENID_URL, dataMap);

        //3、解折结果集
        JSONObject json = JSONUtil.parseObj(result);

        return json.getStr("openid");
    }

    /**
     * 获取手机号
     *
     * @param phonecode
     * @return
     */
    @Override
    public String getPhone(String phonecode) {

//        POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
//        String PHONE_URL ="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
        //1、封装Http请求参数
        String accessToken = getAccessToken();
        PHONE_URL += accessToken;

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("code", phonecode);

        //2、使用糊涂工县实现远程调用
        String result = HttpUtil.post(PHONE_URL, JSONUtil.toJsonStr(dataMap));
        //3、解折结果集
        JSONObject json = JSONUtil.parseObj(result);
        //4获取手机号
        return json.getJSONObject("phone_info").getStr("phoneNumber");
    }

    /**
     * 获取access_Token
     *
     * @return
     */
    public String getAccessToken() {
//        GET https://api.weixin.qq.com/cgi-bin/token
//        String ACCESSTOKEN_URL= "https://api.weixin.qq.com/cgi-bin/token";
        //1、封装Http请求参数
        Map<String, Object> dataMap = getAppConfig();
        dataMap.put("grant_type", "client_credential");
        //2、使用糊涂工县实现远程调用
        String result = HttpUtil.get(ACCESSTOKEN_URL, dataMap);

        //3、解折结果集

        JSONObject json = JSONUtil.parseObj(result);
        return json.getStr("access_token");

    }
}
